﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;

namespace S7Project_One.MVVM
{
    public partial class MainVM : ObservableObject
    {
        [ObservableProperty]
        private string? time;
        [ObservableProperty]
        private string? userName;
        [ObservableProperty]
        private string? avatar;
        [ObservableProperty]
        private string? connectStatusText = "pending";
        [ObservableProperty]
        private string? connectStatusBg = "Gray";
        [ObservableProperty]
        private UIElement mainContent;
        public void NavigateToPage(string pageName)
        {
            // 获取到对应pageName的类型
            Type type = Type.GetType(pageName);
            // 根据pageName的类型来匹配激活页面
            MainContent = Activator.CreateInstance(type) as UIElement;
        }

        public MainVM()
        {
            UserName = "ManSui";
            // 初始化 mainContent 为一个非 null 的默认值
            MainContent = new System.Windows.Controls.TextBlock { Text = "默认页面内容" };
            NavigateToPage("S7Project_One.Views.MonitorView");
            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(500);
                    Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
            });
        }

        [RelayCommand]
        private void MenuItem(Object obj)
        {
            this.NavigateToPage(obj.ToString());
        }
        [RelayCommand]
        private void Close(Window window)
        {
            // 提示弹窗给用户确认是否关闭应用
            MessageBoxResult result = MessageBox.Show("即将退出所有的服务! 确定关闭软件吗?", "按钮操作注意!", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return; // 用户选择不关闭应用，直接返回
            }
            if (window != null)
            {
                window.Close();
            }
        }
        [RelayCommand]
        private void ControlWindowState(Window w)
        {
            if (w != null)
            {
                if (w.WindowState == WindowState.Maximized)
                {
                    w.WindowState = WindowState.Normal;
                }
                else
                {
                    w.WindowState = WindowState.Maximized;
                }
            }
        }

        [RelayCommand]
        private void MinimizedWindowState(Window w)
        {
            if (w != null)
            {
                w.WindowState = WindowState.Minimized;
            }
        }
    }
}
